package com.ys.timer;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwElectronicInvoice;
import com.ys.entity.finance.TcwOperateCompanyPrepayment;
import com.ys.entity.finance.TcwProformaInvoice;
import com.ys.entity.finance.TcwProformaInvoiceItem;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.service.sys.CommonApiService;
import com.ys.util.dhecp.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.*;

/**
 * 每月生成运营形式开票定时器
 */
@Controller
public class TimerTcwProformaInvoiceDaily {
	private static Logger logger = LoggerFactory.getLogger(TimerTcwProformaInvoiceDaily.class);

	@Autowired
	private DiySqlMapper diySqlMapper;

	@Autowired
	private CommonApiService commonApiService;


	/**
     * 每个月2号3点
	 * @throws Exception
	 */
	@Scheduled(cron = "0 0 3 2 * ?")
	public void getProformaInvoiceDaily() throws Exception{
		getProformaInvoice();
	}


	public void getProformaInvoice() throws Exception{
		logger.info("***********每月定时新增运营形式发票开票单getProformaInvoice，开始***********");

		try {
			//			上个月第一天
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, -1);

			Date startTime = calendar.getTime();
			String invoiceMonth = DateUtils.convertDateToTDate(startTime,"yyyy-MM");
			//			这个月的当天
		    calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, 0);

			startTime = calendar.getTime();
			String invoiceMonthDate = DateUtils.convertDateToTDate(startTime,"yyyyMMdd");
			String invoiceDate = DateUtils.convertDateToTDate(startTime,"yyyy-MM-dd");
			//获取所有的运营公司信息
			List<ThrOperateCompanyApply> thrOperateCompanyApplyList = new ThrOperateCompanyApply().setOperationsAttribute(2).queryList();
//
			//查询逸博预付款金额
			List<TcwOperateCompanyPrepayment> tcwOperateCompanyPrepaymentList = new TcwOperateCompanyPrepayment().setVyhzhmc("福州逸博供应链有限公司").queryList();
			BigDecimal preAmount = BigDecimal.ZERO;
			for (TcwOperateCompanyPrepayment tcwOperateCompanyPrepayment : tcwOperateCompanyPrepaymentList){
				preAmount = preAmount.add(tcwOperateCompanyPrepayment.getPreAmount());
			}
//
			List<TcwProformaInvoice> tcwProformaInvoiceList = new ArrayList<>();
			List<TcwProformaInvoiceItem> tcwProformaInvoiceItemList = new ArrayList<>();
			TcwProformaInvoice tcwProformaInvoice;
			int i = 0;
			System.out.println(thrOperateCompanyApplyList.size());
			for (ThrOperateCompanyApply thrOperateCompanyApply : thrOperateCompanyApplyList){
				i++;
				System.out.println(i + thrOperateCompanyApply.getVgsbm());
//				//查询对应运营公司的形式发票流水总计
				Map<String, Object> fjeSumList = diySqlMapper.getFjeSumList(thrOperateCompanyApply.getVgsbm());
				String bid = commonApiService.getCommonNextBid(MenuId.TCW_ProformaInvoice);
				tcwProformaInvoice = new TcwProformaInvoice();
				tcwProformaInvoice.setBid(bid);
				tcwProformaInvoice.setInvoiceMonth(invoiceMonth);
				tcwProformaInvoice.setVgsbmBuy(thrOperateCompanyApply.getVgsbm());
				tcwProformaInvoice.setVgsbmSales("C103");
				tcwProformaInvoice.setInvoiceNumber(thrOperateCompanyApply.getVgsbm() + "-" + invoiceMonthDate);
				tcwProformaInvoice.setInvoiceDate(invoiceDate);

				//添加流水明细
				addTcwProformaInvoiceItemList(tcwProformaInvoiceItemList, fjeSumList,preAmount, tcwProformaInvoice);

				BigDecimal transactionAmount = BigDecimal.ZERO;
				//累计交易金额
				BigDecimal transactionAmount1 = getTransactionAmount(transactionAmount, fjeSumList);
				tcwProformaInvoice.setTransactionAmount(transactionAmount1);
				tcwProformaInvoiceList.add(tcwProformaInvoice);

			}
			//批量新增形式发票主表
			int tcwProformaInvoiceListLength = tcwProformaInvoiceList.size();
			int j = 0;
			while (tcwProformaInvoiceListLength > 50) {
				diySqlMapper.insetBatchTcwProformaInvoice(tcwProformaInvoiceList.subList(j, j + 50));
				j = j + 50;
				tcwProformaInvoiceListLength = tcwProformaInvoiceListLength - 50;
			}
			if (tcwProformaInvoiceListLength > 0) {
				diySqlMapper.insetBatchTcwProformaInvoice(tcwProformaInvoiceList.subList(j, j + tcwProformaInvoiceListLength));
			}

			//批量新增形式发票主表
			int tcwProformaInvoiceItemListLength = tcwProformaInvoiceItemList.size();
			j = 0;
			while (tcwProformaInvoiceItemListLength > 50) {
				diySqlMapper.insetBatchTcwProformaInvoiceItem(tcwProformaInvoiceItemList.subList(j, j + 50));
				j = j + 50;
				tcwProformaInvoiceItemListLength = tcwProformaInvoiceItemListLength - 50;
			}
			if (tcwProformaInvoiceItemListLength > 0) {
				diySqlMapper.insetBatchTcwProformaInvoiceItem(tcwProformaInvoiceItemList.subList(j, j + tcwProformaInvoiceItemListLength));
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		logger.info("***********每月定时新增运营形式发票开票单getProformaInvoice，结束***********");
	}

	/**
	 * 流水详情
	 * @param tcwProformaInvoiceItemList
	 * @param fjeSumList
	 * @return
	 */
	private List<TcwProformaInvoiceItem> addTcwProformaInvoiceItemList(List<TcwProformaInvoiceItem> tcwProformaInvoiceItemList
			,Map<String, Object> fjeSumList,BigDecimal preAmount
			,TcwProformaInvoice tcwProformaInvoice){
		//采购服务→采购成本
		TcwProformaInvoiceItem tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("采购服务→采购成本").setVamount(new BigDecimal(fjeSumList.get("procurementCost").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//采购服务→代理采购服务费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("采购服务→代理采购服务费").setVamount(new BigDecimal(fjeSumList.get("purchasingService").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//仓储服务→仓储费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("仓储服务→仓储费").setVamount(new BigDecimal(fjeSumList.get("storageCharge").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//仓储服务→手续费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("仓储服务→手续费").setVamount(new BigDecimal(fjeSumList.get("serviceCharge").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//仓储服务→透明计划贴标费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("仓储服务→透明计划贴标费").setVamount(new BigDecimal(fjeSumList.get("labelingFee").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//仓储服务→纸箱费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("仓储服务→纸箱费").setVamount(new BigDecimal(fjeSumList.get("cartonFharge").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//仓储服务→组装费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("仓储服务→组装费").setVamount(new BigDecimal(fjeSumList.get("assemblingCharge").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//物流服务→头程运费 (电子票物流服务→头程运费总和)
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("物流服务→头程运费").setVamount(new BigDecimal(fjeSumList.get("firstFreight").toString()).subtract(preAmount).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//产品服务→产品开发费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("产品服务→产品开发费").setVamount(new BigDecimal(fjeSumList.get("productdevelopmentexpense").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→VAT注册费及续期服务费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→VAT注册费及续期服务费").setVamount(new BigDecimal(fjeSumList.get("vatFee").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→经营理赔款项
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→经营理赔款项").setVamount(new BigDecimal(fjeSumList.get("manageClaimsPayments").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→云服务器
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→云服务器").setVamount(new BigDecimal(fjeSumList.get("cloudServer").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→资源占用费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→资源占用费").setVamount(new BigDecimal(fjeSumList.get("resourcesUseFee").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→商标专利注册
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→商标专利注册").setVamount(new BigDecimal(fjeSumList.get("trademarkPatent").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→软件开户费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→软件开户费").setVamount(new BigDecimal(fjeSumList.get("softwareAccountOpening").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//产品服务→美工费用
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("产品服务→美工费用").setVamount(new BigDecimal(fjeSumList.get("artCost").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→管理绩效
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→管理绩效").setVamount(new BigDecimal(fjeSumList.get("managementPerformance").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→管理绩效分配
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→管理绩效分配").setVamount(new BigDecimal(fjeSumList.get("managementPerformanceDistribution").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//运营服务→手续费
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("运营服务→手续费").setVamount(new BigDecimal(fjeSumList.get("yyserviceCharge").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//仓储服务→经营理赔款项
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("仓储服务→经营理赔款项").setVamount(new BigDecimal(fjeSumList.get("manageClaimsPayments").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//组装服务→经营理赔款项
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("组装服务→经营理赔款项").setVamount(new BigDecimal(fjeSumList.get("zzpf").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//物流服务→经营理赔款项
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("物流服务→经营理赔款项").setVamount(new BigDecimal(fjeSumList.get("wlpf").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		//信息服务→经营理赔款项
		tcwProformaInvoiceItem = new TcwProformaInvoiceItem().setVname("信息服务→经营理赔款项").setVamount(new BigDecimal(fjeSumList.get("xxpf").toString()).negate()).setProformaInvoiceBid(tcwProformaInvoice.getBid());
		tcwProformaInvoiceItemList.add(tcwProformaInvoiceItem);

		return tcwProformaInvoiceItemList;
	}

	private BigDecimal getTransactionAmount(BigDecimal transactionAmount,Map<String, Object> fjeSumList){
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("procurementCost").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("purchasingService").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("storageCharge").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("serviceCharge").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("labelingFee").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("cartonFharge").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("assemblingCharge").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("firstFreight").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("productdevelopmentexpense").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("vatFee").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("manageClaimsPayments").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("cloudServer").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("resourcesUseFee").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("trademarkPatent").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("softwareAccountOpening").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("artCost").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("managementPerformance").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("managementPerformanceDistribution").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("yyserviceCharge").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("manageClaimsPayments").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("zzpf").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("wlpf").toString()));
		transactionAmount = transactionAmount.add(new BigDecimal(fjeSumList.get("xxpf").toString()));
		return transactionAmount;
	}
}
